<?php
$loader->requireOnce('includes/RadQueue/ARadQueue.class.php');
class RadWaitingQueue extends ARadQueue {
     public function renderQueue($roomId, $enumPatientType){
        $queueModelList =   $this->buildQueueModel($roomId, $enumPatientType,
            Utils::getEnumKey(EnumerationConstant::RAD_TICKET_STATUS, EnumerationConstant::RAD_TICKET_STATUS_WAITING));
        return $this->renderQueueHTML($queueModelList);
     }

     public function addItem($radTicketId, $roomId) {
        // Get queue object by the ticket
        $objRadQueue    =   ORDataObject::factory('RadQueue')->getObjectByTicket($radTicketId);

        // Set values
        $objRadQueue->setData($radTicketId, $roomId, date('Y-m-d H:i:s'));
        $objRadQueue->persist();

        // Set status of the assignment ticket to 'waiting'
        $objRadTicket   =   ORDataObject::factory('RadTicket')->updateTicketStatus($radTicketId,
            Utils::getEnumKey(EnumerationConstant::RAD_TICKET_STATUS, EnumerationConstant::RAD_TICKET_STATUS_WAITING));
    }

    public function checkQueueItemExist($radTicketId) {
        return ORDataObject::factory('RadQueue')->checkItemExist($radTicketId,
            Utils::getEnumKey(EnumerationConstant::RAD_TICKET_STATUS, EnumerationConstant::RAD_TICKET_STATUS_WAITING));
    }
    
    private function renderQueueHTML($queueModelList) {
        $total  =   count($queueModelList);
        $html   =   '';
        $count  =   1;
        $config = Celini::configInstance();
        $averageTimePerPatient = $config->get('average_time_per_patient');
        foreach ($queueModelList as $queueModel) {
            $limit = $averageTimePerPatient * $count;
            $waiting = $queueModel->getWaitingTime();
            $class = 'normal';
            if ($waiting >= $limit + 10) {
                $class = 'urgent';
            } else if ($waiting >= $limit) {
                $class = 'warning';
            }

            $html .= '<tr>';
            $html .= '<td>' . $count++ . '</td>';
            $html .= '<td><a href="javascript:startRadQueueItem(\'' . $queueModel->getRadTicketId()  . '\');">' . $queueModel->getPatientName() . '</a></td>';
            $html .= '<td>' . $queueModel->getPatientCode() . '</td>';
            $radTicketItem = '';
            foreach ($queueModel->getItemList() as $radItemModel) {
                $radTicketItem .= $radItemModel->getServiceName() . "<br>";
            }
            $html .= '<td>' . $radTicketItem . '</td>';
            $html .= '<td>' . L10nHelper::renderDateTime($queueModel->getStartWaiting(), 'short', 'datetime') . '</td>';
            $html .= "<td class='$class'>" . L10nHelper::_('%s/%s mins', $waiting, $limit). '</td>';
            $html .= '<td>' . $queueModel->getTreatmentDepartmentName() . '</td>';
            $html .= '</tr>';
        }
    	if ($html == '') {
			$html = '<tr class="last"><td colspan="7">' . L10nHelper::_('There is no result') . '</td></tr>';
		}
		$total = '<table cellspacing="0" cellpadding="0" style="width: 100%;">';
        $total .= '<tr class="last"><td>'.L10nHelper::_("Total: <span class='impression'>%s<span>",--$count).'</td></tr>';
        $total .= '</table>';
        return array('body' => $html, 'total_table' => $total);
     }
}
?>